<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\BaseApiController;
use App\Models\QdcsOrg;
use ext\dao\OrgDaoImpl;
use ext\rpc\OrgRpc;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

/**
 * Created by JiangJiaCai.
 * User: Administrator
 * Date: 2017/12/14 0014
 * Time: 10:35
 */
class OrgController extends BaseApiController
{
    //{method template}

    private $orgDao = null;
    private $orgRpc = null;
    public function __construct(Request $request)
    {
//{construct template}
        parent::__construct($request);
        $this->orgDao = new OrgDaoImpl();
        $this->orgRpc = new OrgRpc($this->param ?? []);
    }

    /**
     * 更新
     * @param Request $request
     * @return string
     */
    public function addOrg(Request $request){
        try{
            $this->orgDao->createData(QdcsOrg::class , $this->orgRpc);
        }catch (\Exception $e){
            Log::error('添加组织错误' . $e->getMessage());
            return $this->setJsonError($e->getMessage());
        }
        return $this->setSucceed();
    }

    /**
     * 获取列表
     * @return string
     */
    public function getOrgList(){
        try{
            return $this->setSucceed($this->orgDao->getAllList(QdcsOrg::class));
        }catch (\Exception $e){
            return $this->setJsonError($e->getMessage());
        }
    }

    /**
     * 编辑列表
     * @param int $orgId
     * @return string
     */
    public function editOrg(Request $request){
        try{
            $this->orgRpc->id = $this->param['orgId'];
            if ( !$this->orgDao->updateData(QdcsOrg::class , $this->orgRpc)) throw new \Exception('编辑失败!');
        }catch (\Exception $e){
            Log::error('编辑组织错误' . $e->getMessage());
            return $this->setJsonError($e->getMessage());
        }
        return $this->setSucceed();
    }

    /**
     * 删除
     * @return string
     */
    public function deleteOrg(Request $request){
        try{
            if ( !$this->orgDao->delete(QdcsOrg::class , $this->param['orgId'])) throw new \Exception('delete error');
        }catch (\Exception $e){
            Log::error('删除组织错误' . $e->getMessage());
            return $this->setJsonError($e->getMessage());
        }

        return $this->setSucceed();
    }

}